package com.etone.smartAudit.service;

import com.etone.smartAudit.apiIntercept.Result;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.domain.IapDataModel;
import com.etone.smartAudit.domain.IapDataModelRemark;
import com.etone.smartAudit.error.CustomParameterizedException;
import com.etone.smartAudit.mapper.IapDataModelRemarkMapper;
import com.etone.smartAudit.vo.iapDataModelNav.IapDataModelNavRemarkVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author ：张任豪
 * @date ：2022/2/22 10:33
 * @description：
 */

@Service
@AllArgsConstructor
public class IapDataModelRemarkService {

    private IapDataModelService iapDataModelService;

    private IapDataModelRemarkMapper iapDataModelRemarkMapper;

    public Result addOrUpdateRemark(Integer modelId, Integer remarkId, String remark, Admin admin) {
        if (modelId != null){
            IapDataModel iapDataModel = iapDataModelService.get(modelId);
            if (iapDataModel == null) {
                throw new CustomParameterizedException("元数据不存在");
            }
        }

        //修改操作
        if (remarkId != null){
            IapDataModelRemark modelRemark = iapDataModelRemarkMapper.selectById(remarkId);
            if (!(modelRemark.getCreateId().equals(admin.getId()) || admin.getIsSuper())){
                return Result.fail("你没有权限进行修改");
            }
            modelRemark.setInfo(remark);
            modelRemark.setUpdateId(admin.getId());
            modelRemark.setUpdateName(admin.getNickName());
            modelRemark.setUpdateTime(new Date());
            iapDataModelRemarkMapper.updateById(modelRemark);
            return Result.success("修改成功");
        }

        //新增操作
        IapDataModelRemark modelRemark = IapDataModelRemark.builder()
                .dataModelId(modelId)
                .createId(admin.getId())
                .createName(admin.getNickName())
                .createTime(new Date())
                .info(remark)
                .updateId(admin.getId())
                .updateName(admin.getNickName())
                .updateTime(new Date()).build();
        iapDataModelRemarkMapper.insert(modelRemark);
        return Result.success("添加成功");
    }

    public Result deleteRemark(Integer id, Admin admin) {
        IapDataModelRemark modelRemark = iapDataModelRemarkMapper.selectById(id);

        if (!(modelRemark.getCreateId().equals(admin.getId()) || admin.getIsSuper())){
            return Result.fail("你没有权限进行删除");
        }
        int num = iapDataModelRemarkMapper.deleteById(id);
        return Result.success("成功删除" + num + "条数据");
    }

    public List<IapDataModelNavRemarkVO> list(Integer id, Admin admin) {
        List<IapDataModelNavRemarkVO> list = iapDataModelRemarkMapper.list(id);
        if (!CollectionUtils.isEmpty(list)){
            list.forEach(vo ->{
                if (admin.isAdmin() || admin.getId().equals(vo.getCreateId())){
                    vo.setIsUpdate(true);
                }else {
                    vo.setIsUpdate(false);
                }
            });
        }
        return list;
    }
}
